package de.pelizaeus.app;



import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import de.pelizaeus.app.other.Utils;


public class VTPFragment extends Fragment {

    public VTPFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView =  inflater.inflate(R.layout.fragment_vtp, container, false);
        // Makes Progress bar Visible
        getActivity().getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        if (!Utils.isOnline(getActivity()))
        {
            Toast.makeText(getActivity(), "Keine Internetverbindung erkannt! Bitte stelle eine Verbindung mit dem Internet her, um den VTP anzuzeigen!", Toast.LENGTH_LONG).show();
            getActivity().finish();
        }
        WebView myWebView = (WebView) rootView.findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        final Activity activity = getActivity();
        myWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                CharSequence message = (CharSequence) "Oh no! " + description;
                Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
            }
        });
        myWebView.loadUrl("http://vtp.v22014052188918391.yourvserver.net/Schueler_App.html");
        myWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                //Make the bar disappear after URL is loaded, and changes string to Loading...
                activity.setTitle("Wird geladen...");
                activity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

                // Return the app name after finish loading
                if(progress == 100)
                    activity.setTitle(R.string.activity_name_vtp);
            }
        });
        return rootView;
    }


}
